package cn.maxwell.controller;

import cn.maxwell.User;
import cn.maxwell.block.UserBlock;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 订单服务来调用
 */
@RefreshScope  //刷新配置
@RestController
@RequestMapping("/user")
public class UserController {

    @Value("${server.port}")
    private Integer port;

    @Value("${dev.name}")
    private String name;

    @GetMapping("/loadById/{id}")
    //限流：value="user-service":资源名字：在配置流控的时候在资源名字上配置
    @SentinelResource(value="user-service",blockHandler="exceptionHandler",blockHandlerClass = UserBlock.class)
    public User loadById(@PathVariable("id") Long id){
        User user = new User();
        user.setId(1);
        user.setName("hehe!!!"+name);
        user.setDesc("嚯嚯");
        return user;
    }

    /**
     * sentinel限流：方式一：方法限流
     * @param id
     * @param e
     * @return
     */
    public User exceptionHandler(@PathVariable("id") Long id, BlockException e){
        e.printStackTrace();
        User user = new User(-1, "我已经被限流了", "我已经jre了！你再点我也让你jre了！！！");
        return user;
    }
}
